import 'package:flutter/material.dart';
import '../widgets/platform/adaptive_card.dart';
import '../../core/platform/platform_detector.dart';

/// AI推荐优先级任务组件
class AIRecommendations extends StatelessWidget {
  final List<AIRecommendation> recommendations;
  final String? title;

  const AIRecommendations({
    super.key,
    required this.recommendations,
    this.title,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (title != null) ...[
          SelectableText(
            title!,
            style: Theme.of(context).textTheme.titleLarge,
          ),
          const SizedBox(height: 16),
        ],
        if (PlatformDetector.isDesktop() || PlatformDetector.isWeb())
          GridView.builder(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 3,
              crossAxisSpacing: 16,
              mainAxisSpacing: 16,
              childAspectRatio: 1.2,
            ),
            itemCount: recommendations.length,
            itemBuilder: (context, index) {
              final recommendation = recommendations[index];
              return _buildRecommendationCard(context, recommendation);
            },
          )
        else
          GridView.builder(
            shrinkWrap: true,
            physics: const NeverScrollableScrollPhysics(),
            gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
              crossAxisCount: 1,
              crossAxisSpacing: 16,
              mainAxisSpacing: 16,
              childAspectRatio: 3,
            ),
            itemCount: recommendations.length,
            itemBuilder: (context, index) {
              final recommendation = recommendations[index];
              return _buildRecommendationCard(context, recommendation);
            },
          ),
      ],
    );
  }

  /// 构建推荐卡片
  Widget _buildRecommendationCard(
      BuildContext context, AIRecommendation recommendation) {
    return AdaptiveCard(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SelectableText(
            recommendation.title,
            style: const TextStyle(
              fontSize: 16,
              fontWeight: FontWeight.bold,
            ),
          ),
          const SizedBox(height: 8),
          SelectableText(
            recommendation.description,
            style: const TextStyle(
              fontSize: 14,
              color: Colors.grey,
            ),
            maxLines: 2,
            // overflow: TextOverflow.ellipsis, // SelectableText不支持overflow参数
          ),
          const SizedBox(height: 8),
          Row(
            children: [
              Container(
                padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
                decoration: BoxDecoration(
                  color: recommendation.priorityColor.withValues(alpha: 0.1),
                  borderRadius: BorderRadius.circular(4),
                ),
                child: SelectableText(
                  recommendation.priority,
                  style: TextStyle(
                    color: recommendation.priorityColor,
                    fontSize: 12,
                  ),
                ),
              ),
              const SizedBox(width: 8),
              SelectableText(
                '预计耗时：${recommendation.time}',
                style: const TextStyle(
                  fontSize: 12,
                  color: Colors.grey,
                ),
              ),
            ],
          ),
          const Spacer(),
          SelectableText(
            recommendation.reason,
            style: const TextStyle(
              fontSize: 12,
              color: Colors.blue,
            ),
          ),
          const Spacer(),
          Align(
            alignment: Alignment.bottomRight,
            child: TextButton(
              onPressed: recommendation.onDetail,
              child: const SelectableText('查看详情'),
            ),
          ),
        ],
      ),
    );
  }
}

/// AI推荐任务数据模型
class AIRecommendation {
  final String title;
  final String description;
  final String priority;
  final Color priorityColor;
  final String time;
  final String reason;
  final VoidCallback? onDetail;

  AIRecommendation({
    required this.title,
    required this.description,
    required this.priority,
    required this.priorityColor,
    required this.time,
    required this.reason,
    this.onDetail,
  });
}
